package com.example.mykotilin.workmanager.lib.worker

import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.MediaStore
import android.provider.SyncStateContract
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.example.mykotilin.R
import com.example.mykotilin.workmanager.lib.Constants
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.*

class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParameters) :
    CoroutineWorker(appContext, workerParams) {
    override suspend fun doWork(): Result {
        val resolver = applicationContext.contentResolver
        return try {
            val input = Uri.parse(inputData.getString(Constants.KEY_IMAGE_URI))
            val imageLocation = insertImage(resolver, input)
            if (imageLocation.isNullOrEmpty()) {
                Log.e(TAG, "Writing to MediaStore failed")
                Result.failure()
            }
            val output = Data.Builder().putString(Constants.KEY_IMAGE_URI, imageLocation)
                .build()
            Result.success(output)
        } catch (exception: Exception) {
            Log.e(TAG, "Unable to save image to Gallery", exception)
            Result.failure()
        }
    }

    private fun insertImage(resolver: ContentResolver, resourceUri: Uri): String? {
        val bitmap = BitmapFactory.decodeStream(resolver.openInputStream(resourceUri))
        return MediaStore.Images.Media.insertImage(
            resolver, bitmap, DATE_FORMATTER.format(Date()), TITLE
        )
    }

    override suspend fun getForegroundInfo(): ForegroundInfo {
        return ForegroundInfo(
            NOTIFICATION_ID, createNotification(applicationContext, id,
                applicationContext.getString(R.string.notification_title_saving_image)))
    }

    companion object {
        // Use same notification id as BaseFilter worker to update existing notification. For a real
        // world app you might consider using a different id for each notification.
        private const val NOTIFICATION_ID = 1
        private const val TAG = "SvImageToGalleryWrkr"
        private const val TITLE = "Filtered Image"
        private val DATE_FORMATTER =
            SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z", Locale.getDefault())
    }
}